<?php


namespace App\Http\Library;


class Pfs
{

    /**
     * 2.4排课信息同步>机构端>招行:班级排课列表
     *
     * @param $param
     * @param $pfs
     *
     * @return mixed
     * @throws \Exception
     */
    public static function pfsHeadAddclasslesson($param, $pfs)
    {
        /*接口地址*/
        $url = config('ext.pfs_host') . '/pfs/head/addClassLesson';
        Curl::$url = $url;
        /*通讯安全-加密&签名*/
        Curl::$bodyParam = self::bodyParam($param, $pfs);
        $result = Curl::commit();
        //$result = '{"code":200,"message":"操作成功","data":"+2EcHcvelpqLaYOTBLYR0BZ4YJqz7o4Yipw3Ij93Ebc=","signature":"55773a64d0e269139a8e85b1abce6a8c4f649c608035a6b5342939cdb0bd0217"}';
        /*解析结果*/
        $resultArr = self::result($result, $pfs);
        log_arr([
            '[排课信息同步]',
            'url'       => $url,
            'param'     => $param,
            'result'    => $result,
            'resultArr' => $resultArr,
        ], 'pfs');
        if (isset($resultArr->code) && $resultArr->code == 200 && !empty($resultArr->data)) {
            return $resultArr;
        }
        $msg = empty($resultArr->message) ? $result : json_encode($resultArr, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
        throw new \Exception($msg);
    }


    /**
     * 3.10 班级拨付余额查询  机构端->银行
     *
     * @param $param
     * @param $pfs
     *
     * @return mixed
     * @throws \Exception
     */
    public static function PfsHeadQueryClassBalance($param, $pfs)
    {
        /*接口地址*/
        $url = config('ext.pfs_host') . '/pfs/head/queryClassBalance';
        Curl::$url = $url;
        Curl::$bodyParam = self::bodyParam($param, $pfs);
        $result = Curl::commit();
        /*解析结果*/
        $resultArr = self::result($result, $pfs);
        if (!empty($resultArr->data->classList) && $resultArr->code == 200) {
            return $resultArr->data->classList;
        }
        log_arr([
            '[班级拨付余额查询]',
            'url'       => $url,
            'param'     => $param,
            'result'    => $result,
            'resultArr' => $resultArr,
        ], 'pfs');
        $msg = empty($resultArr->message) ? $result : json_encode($resultArr, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
        throw new \Exception($msg);
    }


    /**
     * 3.8 监管户余额查询   机构端 -> 银行
     *
     * @param $param
     * @param $pfs
     *
     * @return array
     * @throws \Exception
     */
    public static function pfsHeadQueryAccountBalance($param, $pfs)
    {
        /*接口地址*/
        $url = config('ext.pfs_host') . '/pfs/head/queryAccountBalance';
        Curl::$url = $url;
        Curl::$bodyParam = self::bodyParam($param, $pfs);
        $result = Curl::commit();
        /*解析结果*/
        $resultArr = self::result($result, $pfs);
        if (isset($resultArr->code) && $resultArr->code == 200) {
            return empty($resultArr->data) ? [] : $resultArr->data;
        }
        log_arr([
            '[监管户余额查询]',
            'url'       => $url,
            'param'     => $param,
            'result'    => $result,
            'resultArr' => $resultArr,
        ], 'pfs');
        $msg = empty($resultArr->message) ? $result : json_encode($resultArr, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
        throw new \Exception($msg);
    }


    /**
     * 3.7监管户动账查询   机构端 -> 银行
     *
     * @param $param
     * @param $pfs
     *
     * @return array
     * @throws \Exception
     */
    public static function pfsHeadQueryHostDetail($param, $pfs)
    {
        /*接口地址*/
        $url = config('ext.pfs_host') . '/pfs/head/queryHostDetail';
        Curl::$url = $url;
        Curl::$bodyParam = self::bodyParam($param, $pfs);
        $result = Curl::commit();
        /*解析结果*/
        $resultArr = self::result($result, $pfs);
        if (isset($resultArr->code) && $resultArr->code == 200) {
            return empty($resultArr->data) ? [] : $resultArr->data;
        }
        log_arr([
            '[监管户动账查询]',
            'url'       => $url,
            'param'     => $param,
            'result'    => $result,
            'resultArr' => $resultArr,
        ], 'pfs');
        $msg = empty($resultArr->message) ? $result : json_encode($resultArr, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
        throw new \Exception($msg);
    }


    /**
     * 备份金退费借回结果查询   机构端 -> 招行
     *
     * @param $param
     * @param $pfs
     *
     * @return mixed
     */
    public static function pfsHeadQueryWithdrawProvision($param, $pfs)
    {
        /*接口地址*/
        $url = config('ext.pfs_host') . '/pfs/head/queryWithdrawProvision';
        Curl::$url = $url;
        Curl::$bodyParam = self::bodyParam($param, $pfs);
        $result = Curl::commit();
        /*解析结果*/
        $resultArr = self::result($result, $pfs);
        return $resultArr;
    }


    /**
     * 备份金退费借回   机构端 -> 招行
     *
     * @param $param
     * @param $pfs
     *
     * @return mixed
     * @throws \Exception
     */
    public static function pfsHeadWithdrawProvision($param, $pfs)
    {
        /*接口地址*/
        $url = config('ext.pfs_host') . '/pfs/head/withdrawProvision';
        Curl::$url = $url;
        Curl::$bodyParam = self::bodyParam($param, $pfs);
        $result = Curl::commit();
        /*解析结果*/
        $resultArr = self::result($result, $pfs);
        log_arr([
            '[备份金退费借回-同步]',
            'url'       => $url,
            'param'     => $param,
            'result'    => $result,
            'resultArr' => $resultArr,
        ], 'pfs');
        if (isset($resultArr->code) && $resultArr->code == 200 && !empty($resultArr->data)) {
            return $resultArr->data;
        }
        $msg = empty($resultArr->message) ? $result : json_encode($resultArr, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
        throw new \Exception($msg);
    }


    /**
     * 划拨/解冻结果查询   机构端 -> 招行
     *
     * @param $param
     * @param $pfs
     *
     * @return mixed
     * @throws \Exception
     */
    public static function pfsHeadQueryTransferBill($param, $pfs)
    {
        /*接口地址*/
        $url = config('ext.pfs_host') . '/pfs/head/queryTransferBill';
        Curl::$url = $url;
        Curl::$bodyParam = self::bodyParam($param, $pfs);
        $result = Curl::commit();
        //$result = '{"code":1021,"message":"本次划拨尚未完成","data":null,"signature":null}';
        /*解析结果*/
        $resultArr = self::result($result, $pfs);
        return $resultArr;
    }


    /**
     * 提出划拨/解冻   机构端 -> 招行
     *
     * @param $param
     * @param $pfs
     *
     * @return mixed
     * @throws \Exception
     */
    public static function pfsHeadAddBatchTransfer($param, $pfs)
    {
        /*接口地址*/
        $url = config('ext.pfs_host') . '/pfs/head/addBatchTransfer';
        Curl::$url = $url;
        Curl::$bodyParam = self::bodyParam($param, $pfs);
        $result = Curl::commit();
        //$result = '{"code":1006,"message":"机构划拨方式有误, transferType:00","data":null,"signature":null}';
        //$result = '{"code":200,"message":"操作成功","data":"oEwHFydoThnlq9Kt7PuMV8QFhIU4+zgHfuhj7JO8fdo=","signature":"c47ab043d3677795f4e55601e87bbc395594db9259098e760d355d3ca4e4400f"}';
        /*解析结果*/
        $resultArr = self::result($result, $pfs);
        log_arr([
            '[提出划拨&解冻-同步]',
            'url'       => $url,
            'param'     => $param,
            'result'    => $result,
            'resultArr' => $resultArr,
        ], 'pfs');
        return $resultArr;
    }


    /**
     * 售/退课汇总信息同步   机构端 -> 招行
     *
     * @param $param
     * @param $pfs
     *
     * @return mixed
     * @throws \Exception
     */
    public static function pfsHeadAddclassincrement($param, $pfs)
    {
        /*接口地址*/
        $url = config('ext.pfs_host') . '/pfs/head/addClassIncrement';
        Curl::$url = $url;
        Curl::$bodyParam = self::bodyParam($param, $pfs);
        $result = Curl::commit();
        //$result = '{"code":1008,"message":"refundAmt 只能为非负整数;classId 不能包含数字和字母之外的字符;payFee 只能为非负整数;totalPayFee 只能为整数;totalRefundAmt 只能为非负整数;totalRefundFee 只能为非负整数;refundFee 只能为非负整数;payAmt 只能为非负整数;","data":null,"signature":null}';
        //$result = '{"code":200,"message":"操作成功","data":"pFutAHP0QfYlCr5sxGvEvFDoitTYJnH+9G/juXKQ2DyjF5qWnvDrtDKcGostz2pJsqLcEyG7HjNrLN0iJEPKiN2KnDvQY0FQTYYGEUQuBGleFWxO0Q0UERu73akg7jMYkVSOEDHorueoz/4Pb4n1ln+dVLov6ibjuV3ytDkY1gXaZ3RmCunB7b+3pa4WCucDB6apsHTVc8gJeA1wDkCGuQ==","signature":"659e7833831ac6b2164531fbef9ca4109edcd2a545954e7c783d99838e0b7008"}';
        /*解析结果*/
        $resultArr = self::result($result, $pfs);
        log_arr([
            '[当日推送缴退费汇总同步]',
            'url'       => $url,
            'param'     => $param,
            'result'    => $result,
            'resultArr' => $resultArr,
        ], 'pfs');
        if (isset($resultArr->code) && $resultArr->code == 200 && !empty($resultArr->data)) {
            return $resultArr->data;
        }
        $msg = empty($resultArr->message) ? $result : json_encode($resultArr, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
        throw new \Exception($msg);
    }


    /**
     * 班级信息同步   机构端 -> 招行
     *
     * @param $param
     * @param $pfs
     *
     * @return array
     */
    public static function pfsHeadAddBatchClass($param, $pfs)
    {
        /*接口地址*/
        $url = config('ext.pfs_host') . '/pfs/head/addBatchClass';
        Curl::$url = $url;
        /*通讯安全-加密&签名*/
        Curl::$bodyParam = self::bodyParam($param, $pfs);
        $result = Curl::commit();
        //$result = '{"code":200,"message":"操作成功","data":"Kx1ws8AJuK3Y63rX5+66FZXWHorcAejnSFX+jtcVvlyGR4vZuU9chqv0fSIqPUC7J4FZx8Tx4D6mnC26D61OiQ==","signature":"32c39b6d8c2ceb2ff8f79e4793492f56dc1e9e2ef74a1d69811b033e855dc23d"}';
        /*解析结果*/
        $resultArr = self::result($result, $pfs);
        log_arr([
            '[班级信息同步]',
            'url'       => $url,
            'param'     => $param,
            'result'    => $result,
            'resultArr' => $resultArr,
        ], 'pfs');
        if (isset($resultArr->code) && $resultArr->code == 200 && !empty($resultArr->data)) {
            return $resultArr->data;
        }
        $msg = empty($resultArr->message) ? $result : json_encode($resultArr, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
        throw new \Exception($msg);
    }


    /**
     * 招行接口响应结果解析
     *
     * @param $result
     * @param $pfs
     *
     * @return mixed
     */
    private static function result($result, $pfs)
    {
        $result = json_decode($result);
        if (!empty($result->data)) {
            $data = openssl_decrypt($result->data, 'AES-128-ECB', $pfs['pfs_encrypt_private']);
            $result->data = empty($data) ? [] : json_decode($data);
        }
        return $result;
    }


    /**
     * 通讯安全-加密&签名
     *
     * @param $param
     * @param $pfs
     *
     * @return array
     */
    private static function bodyParam($param, $pfs)
    {
        /*生成13位时间戳*/
        list($t1, $t2) = explode(' ', microtime());
        $t = (float) sprintf('%.0f', (floatval($t1) + floatval($t2)) * 1000);
        /*查询指定机构编码信息*/
        $data = openssl_encrypt(json_encode($param), 'AES-128-ECB', $pfs['pfs_encrypt_private']);
        /*加密的数据*/
        $signature = hash_hmac('sha256', "{$data}{$t}", $pfs['pfs_sign_private'], false);
        /*签名*/
        return [
            /*13位时间戳*/
            't'         => $t,
            /*招行分配的机构编号*/
            'agencyId'  => $param['agencyId'],
            /*加密的数据*/
            'data'      => $data,
            /*签名*/
            'signature' => $signature,
        ];
    }


}





/**
 * Psf.php
 *
 * 说明:
 *
 * 修改历史
 * ----------------------------------------
 * 2021/6/4 15:38:00   操作:创建
 **/
